/** Interactive Surface  FYP- 25 Interactive Surface FYP- 25 Interactive Surface FYP- 25
 *
 *  @author Acer
 */
package coordinates;

import java.util.Arrays;

/** Interactive Surface FYP- 25 Interactive Surface FYP- 25 Interactive Surface FYP- 25
 *  General Information about this class -
 *  Comments -
 */
public class ArrayFilter {

    int[] xVal, yVal;
    int size;
    int count = 0;
    boolean filled = false;
    double xTot = 0, yTot = 0;

    public ArrayFilter(int size) {
        this.size = size;
        xVal = new int[size];
        yVal = new int[size];
        copyX = new int[size];
        copyY = new int[size];
        k1=new Kalman();
        k2=new Kalman();

    }

    public int median(int[] a) {
        int asize = a.length;
        //sort the array in increasing order
        Arrays.sort(a);
//        for(int h:a){
//            System.out.print(h+" ");
//        }
        //System.out.println("");
        //if it's odd
        if (asize % 2 == 1) {
            return a[asize / 2];
        } else {
            return ((a[asize / 2] + a[asize / 2 - 1]) / 2);
        }
    }

    public void restore() {
        //System.out.println("restored ");
        xVal = new int[size];
        yVal = new int[size];
         k1=new Kalman();
        k2=new Kalman();
        count = 0;
        //System.out.println("size "+size+","+count);
    }
    int[] copyX, copyY;

    public int[] getMedian(int x, int y) {
        int[] po = new int[2];
        if (count >= size - 1) {
            int kl = count % size;
            xVal[kl] = x;
            yVal[kl] = y;
            // System.out.println("Replaced"+kl+" with "+x+","+y);
            //System.out.println(x + "," + y + " Count=" + count+"kl="+kl);
            System.arraycopy(xVal, 0, copyX, 0, xVal.length);
            System.arraycopy(yVal, 0, copyY, 0, yVal.length);
            po[0] = median(copyX);
            po[1] = median(copyY);
        } else {
            //System.out.println("came here count="+count);
            xVal[count] = x;
            yVal[count] = y;
            po[0] = x;
            po[1] = y;
        }
        count++;
        return po;
    }

    Kalman k1,k2;

    public int[] movingAverage(int x, int y) {
        int[] po = new int[2];
        if (count == 0) {
           k1.initKalman(0.1, 0.2, .1, x);
           k2.initKalman(0.1, 0.2, .1, y);

        } else {// count < size-1
            //System.out.println("came here count="+count);
            k1.kalmanUpdate(x);
            k2.kalmanUpdate(y);

        }
        count++;
        po[0]=(int)k1.kalmanPredict();
        po[1]=(int)k2.kalmanPredict();
        return po;
    }

 public int[] movingAverageKalman(int x, int y) {
        int[] po = new int[2];//this is normal one
        if (count == size - 1) {
            xVal[count] = x;
            yVal[count] = y;
            for (int h = 0; h < xVal.length; h++) {
                xTot += xVal[h];
                yTot += yVal[h];
            }
            po[0] = (int) xTot / size;
            po[1] = (int) yTot / size;
        } else if (count > size - 1) {
            int kl = count % size;
            int lastX = xVal[kl];
            int lastY = yVal[kl];

            xVal[kl] = x;
            yVal[kl] = y;

            //System.out.println(lastX + "," + lastY + " Count=" + count);
            xTot = xTot + x - lastX;
            yTot = yTot + y - lastY;

            po[0] = (int) xTot / size;
            po[1] = (int) yTot / size;

        } else {// count < size-1
            //System.out.println("came here count="+count);
            xVal[count] = x;
            yVal[count] = y;
            xTot = 0;
            yTot = 0;
            for (int k = 0; k <= count; k++) {
                xTot += xVal[k];
                yTot += yVal[k];
            }

//            if (count == 0) {
//                for (int k = 0; k < size; k++) {
//                    xVal[k] = x;
//                    yVal[k] = y;
//                }
//            }
            po[0] = (int) xTot / (count + 1);
            po[1] = (int) yTot / (count + 1);


            xTot = 0;
            yTot = 0;
        }
        count++;
        return po;
    }
    

    public static void main(String[] args) {
        ArrayFilter fir = new ArrayFilter(5);
        int[] pol;

//        pol = fir.getMedian(1, 9);
//        System.out.println(pol[0] + "," + pol[1]);
//        pol = fir.getMedian(2, 8);
//        System.out.println(pol[0] + "," + pol[1]);
//        pol = fir.getMedian(3, 7);
//        System.out.println(pol[0] + "," + pol[1]);
//        pol = fir.getMedian(4, 6);
//        System.out.println(pol[0] + "," + pol[1]);
//        pol = fir.getMedian(5, 9);
//        System.out.println(pol[0] + "," + pol[1]);
//        pol = fir.getMedian(10, -15);
//        System.out.println(pol[0] + "," + pol[1]);
//        pol = fir.getMedian(7, 4);
//        System.out.println(pol[0] + "," + pol[1]);
//        pol = fir.getMedian(8, 3);
//        System.out.println(pol[0] + "," + pol[1]);
//        pol = fir.getMedian(12, 6);
//        System.out.println(pol[0] + "," + pol[1]);
//        pol = fir.getMedian(10, 7);
//        System.out.println(pol[0] + "," + pol[1]);

        pol = fir.movingAverage(1, 9);
        System.out.println(pol[0] + "," + pol[1]);
        pol = fir.movingAverage(2, 10);
        System.out.println(pol[0] + "," + pol[1]);
        pol = fir.movingAverage(3, 12);
        System.out.println(pol[0] + "," + pol[1]);
        pol = fir.movingAverage(4, 13);
        System.out.println(pol[0] + "," + pol[1]);
        pol = fir.movingAverage(5, 13);
        System.out.println(pol[0] + "," + pol[1]);
        pol = fir.movingAverage(6, 8);
        System.out.println(pol[0] + "," + pol[1]);
        pol = fir.movingAverage(7, 7);
        System.out.println(pol[0] + "," + pol[1]);
        pol = fir.movingAverage(10, 6);
        System.out.println(pol[0] + "," + pol[1]);
        pol = fir.movingAverage(20, 4);
        System.out.println(pol[0] + "," + pol[1]);
        pol = fir.movingAverage(15, 3);
        System.out.println(pol[0] + "," + pol[1]);
    }
}
